package com.intellagric.agriecom.controller.agriecom_index;

import com.intellagric.agriecom.module.agriecom_produce.ProduceService;
import com.intellagric.agriecom.module.agriecom_produce_category.AgriecomProduceCategoryService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.module.cms.ContentVo;
import com.intellagric.pojo.AgriecomProduce;
import com.intellagric.pojo.AgriecomProduceCategory;
import com.intellagric.pojo.CmsCategoryContent;
import com.intellagric.pojo.SysOffice;
import com.intellagric.service.module.cms_content.ContentService;
import com.intellagric.service.search.agriecom.AgriecomProductSearch;
import com.intellagric.service.search.agriecom.pojo.AgriecomProductResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class SearchController {
    @Autowired
    private AgriecomProductSearch productSearch;

    @Autowired
    private ProduceService produceService;

    @Autowired
    private ContentService contentService;

    @Autowired
    private AgriecomProduceCategoryService categoryService;

    @Value("${search.advertisementsID}")
    private String adID;

    @RequestMapping("/agriecomIndex/search/addALL")
    @ResponseBody
    public LayuiDataGridResult addALL(){
        return productSearch.addALL();
    }

    /**
     * 关键字和分类搜索
     * @param page
     * @param limit
     * @param categoryId
     * @param keyword
     * @return
     */
    @RequestMapping("/agriecomIndex/search/categoryAndKeyword")
    @ResponseBody
    public LayuiDataGridResult categoryAndKeyword(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "3")int limit, @RequestParam(defaultValue = "") String categoryId, @RequestParam(defaultValue = "") String keyword){
        try {
            keyword=new String(keyword.getBytes("ISO-8859-1"),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        LayuiDataGridResult result= productSearch.keywordSearch(page,limit,categoryId,keyword);

        return result;
    }
    @RequestMapping("/agriecomIndex/search/forward")
    public String searchForward(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "20")int limit, String categoryId, String keyword,RedirectAttributes attributes){
        try {
            keyword=new String(keyword.getBytes("ISO-8859-1"),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try {
            keyword=URLEncoder.encode(keyword,"UTF-8");
            keyword=URLEncoder.encode(keyword,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return "redirect:/collection.html?keyword="+keyword+"&categoryId="+categoryId;



    }

    @RequestMapping("/agriecomIndex/search/hots")
    @ResponseBody
    public List<AgriecomProduce> getHotProducts(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "20")int limit){
        return produceService.getHotList(page,limit);
    }

    /**
     * 根据父类id查找所有字类的所有产品
     * @param page
     * @param limit
     * @param categoryId
     * @return
     */
    @RequestMapping("/agriecomIndex/search/getProByParentId")
    @ResponseBody
    public LayuiDataGridResult getProByParentId(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "20")int limit,String categoryId){
        List<AgriecomProduceCategory> list=categoryService.getCategoryChildren(categoryId);

        List<AgriecomProduce> result=new ArrayList<>();
        result.addAll(produceService.getListByCategory(categoryId).getData());
        if (list!=null) {
            for (AgriecomProduceCategory category : list) {
                List<AgriecomProduce> products=produceService.getListByCategory(category.getId()).getData();
                if(products!=null)
                    result.addAll(products);
            }
        }
        //手动分页
        if((page-1)*limit+limit<result.size())
            return LayuiDataGridResult.success().add(result.subList((page-1)*limit,(page-1)*limit+limit),result.size());
        return LayuiDataGridResult.success().add(result.subList((page-1)*limit,result.size()),result.size());
    }

    /**
     * 根据父id重定向到collection页面
     * @param page
     * @param limit
     * @param categoryId
     * @return
     */
    @RequestMapping("/agriecomIndex/search/parentForward")
    public String parentForward(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "20")int limit,String categoryId){
        return "redirect:/collection.html?categoryId="+categoryId;
    }


    /**
     * 根据分类id重定向到参数页面
     * @param page
     * @param limit
     * @param categoryId
     * @return
     */
    @RequestMapping("/agriecomIndex/search/paramForward")
    public String paramForward(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "20")int limit,String categoryId){
        return "redirect:/collectionThird.html?categoryId="+categoryId;
    }
    /**
     * 第二页面
     * @param page
     * @param limit
     * @param categoryId
     * @return
     */
    @RequestMapping("/agriecomIndex/search/secondForward")
    public String secondForward(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "20")int limit,String categoryId){
        return "redirect:/collectionSecond.html?categoryId="+categoryId;
    }

    //@RequestBody() String param   ?categoryId=1&map=[select1:%20"凯特芒",%20select2:%20null,%20select3:%20null] , ,@RequestBody Map<String,String> map
    @RequestMapping("/agriecomIndex/search/paramPageSearch")
    @ResponseBody
    public LayuiDataGridResult paramPageSearch(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "20")int limit,String categoryId,@RequestBody Map<String,String> map){
         return productSearch.paramSearch(page,limit,categoryId,map);
    }


    /**
     * 获取搜索后的ad模块
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/agriecomIndex/search/ad")
    @ResponseBody
    public List<CmsCategoryContent> ad(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "3")int limit){
        ContentVo vo=new ContentVo();
        vo.setCategoryId(adID);
        return contentService.queryContentByVo(page,limit,vo).getData();
    }



}
